package com.zmn.plat.admin.controller.channel.servicearea;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.channel.*;
import com.zmn.plat.services.interfaces.channel.ChannelServiceAreaService;

@Controller
@RequestMapping("/channel/service/area/")
public class ChannelServiceAreaListController extends ChannelBaseController {
    @Resource
    ChannelServiceAreaService channelServiceAreaService;

    private Logger logger = LoggerFactory.getLogger(ChannelServiceAreaListController.class);

    /**
     * 渠道服务区域列表
     *
     * @param query
     *            the query
     * @return the model and view
     */
    @RequestMapping(value = "list")
    @RequiresPermissions("/channel/serviceArea/addEditView.action")
    public ModelAndView list(ChannelServiceAreaQuery query, String addFlag) {

        ModelAndView mav = new ModelAndView("channel/servicearea/list");

        List<ChannelServiceArea> items = channelServiceAreaService.listPageByQuery(query);
        List<BaseArea> provinces = baseAreaService.listByParentId(GlobalConsts.TOP_ID);

        List<BaseArea> citys = Collections.emptyList();
        if (NumberUtil.isPositiveInteger(query.getProvinceId())) {
            citys = baseAreaService.listByParentId(query.getProvinceId());
        }

        List<BaseArea> countrys = Collections.emptyList();
        if (NumberUtil.isPositiveInteger(query.getCityId())) {
            countrys = baseAreaService.listByParentId(query.getCityId());
        }
        Channel obj = channelService.findByKey(query.getChannelId());
        Integer mergeSettle = getMergeSettle(query.getChannelId());
        obj.setMergeSettle(mergeSettle);
        mav.addObject("obj", obj);
        mav.addObject("query", query);
        mav.addObject("provinces", provinces);
        mav.addObject("citys", citys);
        mav.addObject("countrys", countrys);
        mav.addObject("items", items);
        mav.addObject("flag", 4);
        mav.addObject("addFlag", addFlag);
        return mav;
    }

    /**
     * 地图范围页面
     *
     * @param serviceAreaId
     * @return
     */
    @RequestMapping(value = "drawMap")
    @RequiresPermissions("/channel/serviceArea/drawMap/addEditView.action")
    public ModelAndView drawMap(@RequestParam(value = "serviceAreaId", defaultValue = "0") Integer serviceAreaId, String addFlag) {
        ChannelServiceArea channelServiceArea = channelServiceAreaService.findByKey(serviceAreaId);
        ChannelServiceAreaVO channelServiceAreaVO = new ChannelServiceAreaVO();
        BeanUtils.copyProperties(channelServiceArea, channelServiceAreaVO);

        BaseArea province = baseAreaService.findByKey(channelServiceArea.getProvinceId());
        BaseArea city = baseAreaService.findByKey(channelServiceArea.getCityId());

        if (NumberUtil.isPositiveInteger(channelServiceArea.getCountryId())) {
            BaseArea baseArea = baseAreaService.findByKey(channelServiceArea.getCountryId());
            channelServiceAreaVO.setBaseAreaLatitude(baseArea.getLatitude());
            channelServiceAreaVO.setBaseAreaLongitude(baseArea.getLongitude());
            StringBuilder sb = new StringBuilder();
            channelServiceAreaVO.setAreaName(sb.append(province.getName()).append(city.getName()).append(baseArea.getName()).toString());
        } else {
            List<BaseArea> baseAreaList = baseAreaService.listByParentId(city.getAreaId());
            if (!CollectionUtils.isEmpty(baseAreaList)) {
                channelServiceAreaVO.setBaseAreaLatitude(baseAreaList.get(0).getLatitude());
                channelServiceAreaVO.setBaseAreaLongitude(baseAreaList.get(0).getLongitude());
            }
            StringBuilder sb = new StringBuilder();
            channelServiceAreaVO.setAreaName(sb.append(province.getName()).append(city.getName()).toString());
        }

        ModelAndView mav = new ModelAndView("channel/servicearea/baiduMap");
        Channel obj = channelService.findByKey(channelServiceArea.getChannelId());
        mav.addObject("area", channelServiceArea);
        mav.addObject("obj", obj);
        mav.addObject("channelServiceAreaVO", channelServiceAreaVO);
        mav.addObject("flag", 2);
        mav.addObject("serviceAreaId", serviceAreaId);
        mav.addObject("addFlag", addFlag);
        return mav;
    }

    /**
     * 区域服务分类
     *
     * @param serviceAreaId
     *            the service area id
     * @param showType
     *            产品分类 1后台 2前台
     * @return the model and view
     */
    @RequestMapping(value = "serviceCategory")
    @RequiresPermissions("/channel/serviceArea/serviceCategory/addEditView.action")
    public ModelAndView serviceCategory(Integer serviceAreaId, Integer showType, String addFlag) {
        ModelAndView mav = new ModelAndView("channel/servicearea/serviceCategory");

        ChannelServiceArea byKey = channelServiceAreaService.findByKey(serviceAreaId);
        Integer channelId = byKey.getChannelId();

        List<KvDTO<Integer, String>> servCategorys = channelBService.listAllLevelCategoryByChannelId(channelId, showType);
        List<ChannelServiceAreaCategory> serviceCategoryList = areaCategoryService.listByServiceAreaId(serviceAreaId, showType);
        List<String> categIds = serviceCategoryList.stream().map(p -> p.getServCategId() + "-" + p.getCategOneId() + "-" + p.getCategId())
            .collect(Collectors.toList());
        Channel obj = channelService.findByKey(channelId);
        if (servCategorys != null && servCategorys.size() > 0) {
            mav.addObject("currentServCategId", servCategorys.get(0).getKey());
        }
        mav.addObject("obj", obj);
        mav.addObject("area", byKey);
        mav.addObject("categIds", categIds);
        mav.addObject("servCategorys", servCategorys);
        mav.addObject("channelId", channelId);
        mav.addObject("serviceAreaId", serviceAreaId);
        mav.addObject("flag", 1);
        mav.addObject("showType", showType);
        mav.addObject("addFlag", addFlag);
        return mav;
    }

    @RequestMapping(value = "batchServiceCategory")
    @RequiresPermissions("/channel/serviceArea/serviceCategory/addEditView.action")
    public ModelAndView batchServiceCategory(Integer channelId, String serviceAreaIds, Integer showType, String addFlag) {
        ModelAndView mav = new ModelAndView("channel/servicearea/batch/serviceCategory");

        List<KvDTO<Integer, String>> servCategorys = channelBService.listAllLevelCategoryByChannelId(channelId, showType);
        Channel obj = channelService.findByKey(channelId);
        if (servCategorys != null && servCategorys.size() > 0) {
            mav.addObject("currentServCategId", servCategorys.get(0).getKey());
        }
        mav.addObject("obj", obj);
        mav.addObject("servCategorys", servCategorys);
        mav.addObject("channelId", channelId);
        mav.addObject("serviceAreaIds", serviceAreaIds);
        mav.addObject("flag", 1);
        mav.addObject("showType", showType);
        mav.addObject("addFlag", addFlag);
        return mav;
    }
}
